package com.hot12345.common.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServletUtils {
	private static final Log logger = LogFactory.getLog(ServletUtils.class);
	private static StringBuffer sb = new StringBuffer();

	
	/**
	 * 将req参数设置到Map对象中
	 * @param req
	 * @param keys
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	public static Map<String, String> setValuesToMap(
			HttpServletRequest req, Enumeration<String> paramsNames) throws Exception {
		if (paramsNames == null)
			return null;
		Map<String, String> value = new HashMap<String, String>();
		String paramsName = null;
		while (paramsNames.hasMoreElements()) {
			paramsName = paramsNames.nextElement();
			value.put(paramsName, req.getParameter(paramsName));
		}
		return value;
	}
	
	/**
	 * 将req参数设置到Map对象中
	 * @param req
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	public static Map<String, String> setValuesToMap(
			HttpServletRequest req){
		if (req == null || req.getParameterNames()==null){
			logger.error("req == null || req.getParameterNames()==null--------req="+req);
			return null;
		}
		
		Map<String, String> value = new HashMap<String, String>();
		Enumeration<String> paramsNames = req.getParameterNames();
		String paramsName = null;
		while (paramsNames.hasMoreElements()) {
			paramsName = paramsNames.nextElement();
			value.put(paramsName, req.getParameter(paramsName));
		}
		return value;
	}

	public static void showDebug(Map<String, String> valueMap) {
		for (Iterator<String> iter=valueMap.keySet().iterator();iter.hasNext();) {
			String key = iter.next();
			logger.debug("paramterName : " + key + "  value: "
					+ valueMap.get(key));
		}
		logger.debug("theSetValueProperties: " + sb.toString());
	}
}
